package com.dz51.core.basedao.interceptor;

import java.io.Serializable;
import java.util.Date;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;

import com.dz51.core.log4j.ILog4jManager;
/**
 * @Title: EntityInterceptorSupport.java
 * @Package com.dz51.core.basedao.interceptor
 * @Description: Hibernate过滤器
 * @author Zhaoqt  
 * @date 2014-5-10 上午5:31:02
 * @version V1.0
 */
public class EntityInterceptorSupport extends EmptyInterceptor {

	private static final long serialVersionUID = 7319416231145791577L;
	private static final String CREATE_DATE = "createdate";// "创建日期"属性名称
	private static final String MODIFY_DATE = "modifydate";// "修改日期"属性名称
	
	@Autowired
	protected ILog4jManager log4jManager;

	// 保存数据时回调此方法
	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
		Date createdate = new Date();
		for (int i = 0; i < propertyNames.length; i++) {
			if (CREATE_DATE.equals(propertyNames[i]) || MODIFY_DATE.equals(propertyNames[i])) {
				state[i] = createdate;
			}
		}
		return true;
	}

	// 删除数据时回调此方法
	@Override
	public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
		log4jManager.infoCustomLog(getClass().getName(), "onDelete", "Delete Serializable ID:" + id);
	}

	// 更新数据时回调此方法
	@Override
	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
			String[] propertyNames, Type[] types) {
		Date updateDate = new Date();
		for (int i = 0; i < propertyNames.length; i++) {
			if (MODIFY_DATE.equals(propertyNames[i])) {
				currentState[i] = updateDate;
			}
		}
		return true;
	}
}
